package com.hw.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
public final class PropertiesUtils {
	private static String propName="/red5.properties";//默认配置文件路径
	private static String value="";
	/**
	 * 获取配置文件值
	 * @param key
	 * @return
	 */
	public static  String  getProperty(String key) {
			try {
				Properties prop=new Properties();
				InputStream inStream=PropertiesUtils.class.getResourceAsStream(propName);
				prop.load(inStream);
				value=StringUtils.null2String(prop.getProperty(key));
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			return value;
		
	}
	/**
	 * 设置配置文件值
	 * @param key
	 * @return
	 */
	public static  String  writeProperty(String key,String value) {
			try {
				Properties prop=new Properties();
				InputStream inStream=PropertiesUtils.class.getResourceAsStream(propName);
				prop.load(inStream);
				inStream.close();
				OutputStream out=new FileOutputStream(propName);
				prop.setProperty(key, value);
				prop.store(out, "update "+key+" value");
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			return value;
		
	}
	public static  String  getData(String key) {
		try {
			Properties prop=new Properties();
			InputStream inStream=new FileInputStream(propName);
			prop.load(inStream);
			value=StringUtils.null2String(prop.getProperty(key));
			inStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return value;
	
}
	public static void writeData(String key, String value) {   
        Properties prop = new Properties();   
        try {   
            OutputStream fos = new FileOutputStream(propName);   
            prop.setProperty(key, value);   
            prop.store(fos, "Update '" + key + "' value");              
            fos.close();   
        } catch (IOException e) {   
           
        }
    }
	public static void main(String[] args){
		String path=PropertiesUtils.class.getResource("/").getPath();
		PropertiesUtils.setPropName("/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp3/wtpwebapps/zfjly/WEB-INF/classes/timerTask.properties");
        PropertiesUtils.writeData("time", "2016/01/22 16:37:09");
	}
	public static String getPropName() {
		return propName;
	}
	/**
	 * 设置配置文件路径
	 * @param propName
	 */
	public static void setPropName(String propName) {
		PropertiesUtils.propName = propName;
	}

}
